%% Cannon Game
% By: John M. Murray

%% Init
%

tic
close all;
clear all; 
i=0;
G=6.67428e-11; % m^(3) * kg^(-1) * s^(-2)
m_e=5.9e24;
step_size=1;

%% Setup


%% User input
launch(1).x=input('X-Vel\n:>');
launch(1).y=input('Y-Vel\n:>');

if((isempty(launch(1).x)) | (isempty(launch(1).y)))  %#ok<OR2>
	error('Need inputs!');
end

%% Simulate!

%%
% Draw Earth
figure(1);
axis equal;
hold;
t=0:2*pi/1000:2*pi;
plot(6378*1e3*sin(t),6378*1e3*cos(t));

%%
% Make the Ball

ball.fig=plot(0,6378*1e3,'.');
ball.loc=[0 6378*1e3+100];
ball.vel=[launch(1).x launch(1).y];
ball.line=plot(ball.loc(1),ball.loc(2));

%% Loop
%
while(norm(ball.loc)>6378*1e3)
	i=i+1;
	%% Calculate accelerations
	%
	a=G * m_e / norm(ball.loc)^2;
	accel=a * -ball.loc / norm(-ball.loc);
	
	%% Calculate new Velocity
	ball.vel= ball.vel + accel * step_size;
	
	%% Move point
	%
	ball.loc=ball.loc + ball.vel * step_size;
	set(ball.fig,'xData',ball.loc(1),...
				 'yData',ball.loc(2));
			
	%% Update Line
	%
	
	set(ball.line,...
		'xData',cat(2,get(ball.line,'xData'),ball.loc(1)),...
		'yData',cat(2,get(ball.line,'yData'),ball.loc(2)));
	
	
	%% Regraph
	title(sprintf('Ball Vel=[%.2d %.2d]',ball.vel(1),ball.vel(2)));
	drawnow;
	
end



%% Results

%% Fin
toc;